package com.emm.yixun.website.controller.system;

//框架包
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;




//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.ZNodes;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
//变化的引用
import com.emm.yixun.common.model.BaseCodeType;
import com.emm.yixun.common.model.dto.BaseCodeTypeDto;
import com.emm.yixun.common.model.dto.BaseCodeValueDto;
import com.emm.yixun.common.model.enums.CodeTypeEnum;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.system.service.BaseCodeTypeServiceFacade;
import com.emm.yixun.system.service.BaseCodeValueServiceFacade;

@Controller
@RequestMapping("/system/baseCodeType/")
public class BaseCodeTypeController extends BaseController<BaseCodeType> {

	@Autowired
	private BaseCodeTypeServiceFacade baseCodeTypeServiceFacade;
	
	@Autowired
	private BaseCodeValueServiceFacade baseCodeValueServiceFacade;

	private static final String SECURITY_URL = "/system/baseCodeType/index";

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			return "/system/basecode/basecodetype/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	// 新增返回实体对象
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(BaseCodeTypeDto dto) {
		AjaxRes ar = getAjaxRes();

		/*
		 * 1.判断权限2.调用新增接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,
				"system:baseCodeType:add"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				dto.setMerchantId(user.getMerchantId());
				dto.setCreateUserId(user.getId());
				dto.setDelFlag("0");
				dto.setUpdateUserId(user.getId());
				dto.setUpdateTime(new Date());
				Response<BaseCodeTypeDto> response = baseCodeTypeServiceFacade
						.save(dto);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				} else {
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
		}
		return ar;
	}

	// 查看
	@RequestMapping(value = "view", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes view(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,
				"system:baseCodeType:view"))) {
			try {
				Response<BaseCodeType> response = baseCodeTypeServiceFacade
						.findById(id);
				if (response.isSuccess()) {
					ar.setSucceed(response.getEntity());
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	// 修改
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes edit(BaseCodeTypeDto dto) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用修改接口3.返回信息
		 */
		// if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"system:baseCodeType:edit"))){
		try {
			Response<Integer> response = baseCodeTypeServiceFacade
					.updateByPrimaryKey(dto, true);
			if (response.isSuccess()) {
				ar.setSucceedMsg(Const.UPDATE_SUCCEED);
			} else {
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.UPDATE_FAIL);
		}
		// }
		return ar;
	}

	// 删除
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes delete(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,
				"system:baseCodeType:delete"))) {
			try {
				Response<Integer> response = baseCodeTypeServiceFacade
						.deleteByPrimaryKey(id);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				} else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
		}
		return ar;
	}

	// 分页查询
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes list(BaseCodeTypeDto dto, Integer pageSize, Integer pageNum) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,
				"system:baseCodeType:list"))) {
			try {
				Page<BaseCodeType> response = baseCodeTypeServiceFacade
						.findPageByDto(dto, PageUtil.begin(pageNum, pageSize),
								pageSize);
				if (response.getSuccess()) {
					if (null == response.getRows())
						response.setRows(new ArrayList<BaseCodeType>());
					response.setPageNumber(pageNum);
					ar.setSucceed(response);
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	@RequestMapping(value = "getBaseCodeTypeTree", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes getBaseCodeTypeTree(BaseCodeTypeDto baseCodeTypeDto) {
		AjaxRes ar = getAjaxRes();
		try {
			User user = ShiroUtil.getCurrentUser();
			baseCodeTypeDto.setMerchantId(user.getMerchantId());
			baseCodeTypeDto.setOrders("t.sort");
			ResponseList<BaseCodeType> responseList = baseCodeTypeServiceFacade
					.findByDto(baseCodeTypeDto);
			if (!responseList.isSuccess()) {
				ar.setFailMsg(Const.DATA_FAIL);
				if (StringUtils.isNotEmpty(responseList.getResultMessage())) {
					ar.setFailMsg(responseList.getResultMessage());
				}
				return ar;
			}
			List<BaseCodeType> codeTypeList = responseList.getEntity();
			if (null == codeTypeList) {
				responseList.setEntity(new ArrayList<BaseCodeType>());
			}

			// 遍历枚举
			List<ZNodes> r = new ArrayList<ZNodes>();
			int other = 1;
			for (CodeTypeEnum ctEnum : CodeTypeEnum.values()) {
				ZNodes znode = new ZNodes();
				znode.setId(ctEnum.getNo());
				znode.setpId("");
				znode.setName(ctEnum.getDes());
				znode.setOther(String.valueOf(other));
				other += 1;
				r.add(znode);
			}
			if (null != codeTypeList) {
				for (BaseCodeType baseCodeType : codeTypeList) {
					ZNodes znode = new ZNodes();
					znode.setId(String.valueOf(baseCodeType.getId()));
					znode.setpId(baseCodeType.getCodeType());
					znode.setName(baseCodeType.getTypeName());
					znode.setOther(String.valueOf(baseCodeType.getSort()));
					r.add(znode);
				}
			}

			ar.setSucceed(r);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * 
	 * 方法描述 : 查询业态参数 
	 * 创建者：ThinkPad 
	 * 项目名称： emm-website 
	 * 类名：BaseCodeTypeController.java 
	 * 版本： v1.0 
	 * 创建时间： 2017年4月13日 下午2:40:56
	 * @return List<BaseCodeType>
	 */
	@RequestMapping(value = "findBusList")
	public @ResponseBody List<BaseCodeType> findBusList() {
		try {
			BaseCodeTypeDto baseCodeTypeDto = new BaseCodeTypeDto();
			baseCodeTypeDto.setCodeType(CodeTypeEnum.CUSTOMER_02.getNo());
			baseCodeTypeDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			ResponseList<BaseCodeType> responseList = baseCodeTypeServiceFacade.findByDto(baseCodeTypeDto);
			if (responseList.isSuccess()) {
				return responseList.getEntity();
			}
		} catch (Exception e) {
			logger.error("findBusList error: ", e);
		}
		return null;
	}
	/**
	* 方法描述 : 根据typeId,查询BaseValue
	* 创建者：ThinkPad 
	* 项目名称： emm-website
	* 类名： BaseCodeTypeController.java
	* 版本： v1.0
	* 创建时间： 2017年4月13日 下午2:45:29
	* @param typeId
	* @return List<BaseCodeValue>
	 */
	@RequestMapping(value = "findBaseCodeValue")
	public @ResponseBody List<BaseCodeValue> findBaseCodeValue(Long typeId){
		try {
			if(typeId!=null){
				BaseCodeValueDto dto = new BaseCodeValueDto();
				dto.setTypeId(typeId);
				dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
				ResponseList<BaseCodeValue> responseList = baseCodeValueServiceFacade.findByDto(dto);
				if(responseList.isSuccess()){
					return responseList.getEntity();
				}
			}
		
		} catch (Exception e) {
			logger.error("findBaseCodeValue error: ", e);
		}
		return null;
	}
}
